/*
 * Created by Jaren at 2021/12/30 17:42
 */
import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/base/vehicle_data.dart';
import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/base/vehicle_real_time_info.dart';
import 'package:inmotion_protocol/lorin/bean/scooter/scooter_real_time_info.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class S1AllRealTimeInfo extends ScooterAllRealTimeInfo {
  S1AllRealTimeInfo(S1RealTimeInfo realTimeInfo, S1RealTimeState realTimeState, S1RealTimeError realTimeError) : super(realTimeInfo, realTimeState, realTimeError);
}

class S1VehicleSynthesizeRealTimeInfo extends VehicleSynthesizeRealTimeInfo<CommonVehicleRealTimeInfo, S1RealTimeState, S1RealTimeError> {
  S1VehicleSynthesizeRealTimeInfo(CommonVehicleRealTimeInfo commonVehicleRealTimeInfo, S1RealTimeState realTimeState, S1RealTimeError realTimeError) : super(commonVehicleRealTimeInfo, realTimeState, realTimeError);
}

class S1RealTimeInfo extends ScooterRealTimeInfo {
  S1RealTimeInfo(ProductModel model, VersionWrapper version) : super(model, version);
}

class S1RealTimeState extends ScooterRealTimeState {
  S1RealTimeState(ProductModel model, VersionWrapper version) : super(model, version);
}

class S1RealTimeError extends ScooterRealTimeError {
  S1RealTimeError(ProductModel model, VersionWrapper version) : super(model, version);

  /// 姿态传感器故障标识
  Uint4 imuSensorState = Uint4(0);

  /// 倾倒警告标识
  Uint4 fallDownState = Uint4(0);

  @override
  void initCompact() {
    compact = DefaultRealTimeErrorCompact();
  }
}

class DefaultRealTimeErrorCompact extends Compact<S1RealTimeError> {
  @override
  void fromBytes(ScooterRealTimeError output, Uint8ClampedList bytes, int offset) {
    if (bytes.length < offset + length()) {
      return;
    }
    output.controllerComState = ByteUtil.bitValue(bytes[offset], 0, 1);
    output.motorHallState = ByteUtil.bitValue(bytes[offset], 1, 1);
    output.batteryState = ByteUtil.bitValue(bytes[offset], 2, 1);
    output.voltageState = ByteUtil.bitValue(bytes[offset], 3, 2);
    output.breakState = ByteUtil.bitValue(bytes[offset], 5, 1);
    output.throttleState = ByteUtil.bitValue(bytes[offset], 6, 1);
    output.hmicRxState = ByteUtil.bitValue(bytes[offset], 7, 1);
    offset++;
    output.mcRxState = ByteUtil.bitValue(bytes[offset], 0, 1);
    output.overMosTempState = ByteUtil.bitValue(bytes[offset], 1, 1);
    output.overMotorTempState = ByteUtil.bitValue(bytes[offset], 2, 1);
    output.overBatteryTempState = ByteUtil.bitValue(bytes[offset], 3, 1);
    output.mcHeartbeatState = ByteUtil.bitValue(bytes[offset], 4, 1);
    // 保留3bit
  }

  @override
  int length() {
    return 2;
  }
}
